package net.bblot.lottery.util;

import android.content.Context;
import android.text.TextUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * Created by feng on 2017/9/7.
 */

public class ZipUtil {
    private static final String TAG = "ZipUtil";

    private static boolean unZipFile(Context context, InputStream inputStream, String destDir) {
        File zipfileDir = new File(destDir);
        if (zipfileDir.isFile()) {
            return false;
        }
        if (!zipfileDir.exists()) {
            zipfileDir.mkdirs();
        }
        InputStream stream = null;
        ZipInputStream inZip = null;
        try {
            stream = inputStream;
            //将字节读取流转成zip读取流
            inZip = new ZipInputStream(stream);
            //压缩文件实体
            ZipEntry zipEntry;
            //压缩文件实体中的文件名称
            String szName = "";
            while ((zipEntry = inZip.getNextEntry()) != null) {
                szName = zipEntry.getName();
                if (zipEntry.isDirectory()) {
                    //zipEntry是目录,则创建目录
                    szName = szName.substring(0, szName.length() - 1);
                    File folder = new File(zipfileDir, szName);
                    folder.mkdirs();
                } else {
                    //否则创建文件,并输出文件的内容
                    File file = new File(zipfileDir, szName);
                    file.createNewFile();
                    FileOutputStream out = new FileOutputStream(file);
                    int len;
                    byte[] buffer = new byte[1024];
                    while ((len = inZip.read(buffer)) != -1) {
                        out.write(buffer, 0, len);
                        out.flush();
                    }
                    out.close();
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inZip != null) {
                try {
                    inZip.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    /**
     * 解压assets目录下的zip文件
     *
     * @param context
     * @param fileName
     * @param destDirName
     */
    public static boolean unZipAssetsFile(Context context, String fileName, String destDirName) {
        try {
            InputStream inputStream = context.getAssets().open(fileName);
            return unZipFile(context, inputStream, destDirName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean unZipFile(Context context, String zipFilePath, String destDir) {
        if (TextUtils.isEmpty(zipFilePath)) {
            return false;
        }
        try {
            InputStream inputStream = new FileInputStream(zipFilePath);
            return unZipFile(context, inputStream, destDir);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 给定根目录，返回一个相对路径所对应的实际文件名.
     *
     * @param baseDir     指定根目录
     * @param absFileName 相对路径名，来自于ZipEntry中的name
     * @return java.io.File 实际的文件
     */
    public static File getRealFileName(String baseDir, String absFileName) {
        Logger.i(TAG, "baseDir=" + baseDir + "------absFileName="
                + absFileName);
        absFileName = absFileName.replace("\\", "/");
        Logger.i(TAG, "absFileName=" + absFileName);
        String[] dirs = absFileName.split("/");
        Logger.i(TAG, "dirs=" + dirs);
        File ret = new File(baseDir);
        String substr = null;
        if (dirs.length > 1) {
            for (int i = 0; i < dirs.length - 1; i++) {
                substr = dirs[i];
                ret = new File(ret, substr);
            }

            if (!ret.exists())
                ret.mkdirs();
            substr = dirs[dirs.length - 1];
            ret = new File(ret, substr);
            return ret;
        } else {
            ret = new File(ret, absFileName);
        }
        return ret;
    }

    public interface UnZipListener {
        void onUnZipPercentUpdate(int per);
    }
}
